//
//  JCWLogInVC.swift
//  HengYouCai
//
//  Created by wll on 2017/12/26.
//  Copyright © 2017年 lwj. All rights reserved.
//  登录/注册界面--代码

import UIKit

class JCWLogInVC: BaseViewController {
    
    deinit {
        debugPrint("JCWLogInVC dealloc")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let str = WLFileManager.readUserData(forKey: kUserAccount) as? String {

            //触发RAC监听
            self.phoneNumberView.tF.sendActions(for: .editingChanged)
            //触发代理 显示提示文字   放在这里(viewDidAppear)修复 提示文字位置不对bug
            _ = self.phoneNumberView.textField(self.phoneNumberView.tF, shouldChangeCharactersIn: NSRange(location: 0, length: str.count), replacementString: str)
            _ = self.phoneNumberView.textFieldDidEndEditing(self.phoneNumberView.tF)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //不影响注册界面标题颜色
//        self.wl_navTitle("", titleColor: UIColor.black)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.createNav()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        /// 不要放viewdidappear里面 防止返回之后 重新赋值
        if let str = WLFileManager.readUserData(forKey: kUserAccount) as? String {
            //赋值上次登录手机号
        self.phoneNumberView.tF.text = str
        }
        self.createCV()
        let leftBarBtn = UIButton(type: .custom)
        leftBarBtn.setImage(UIImage(named: "back_white"), for: .normal)
        leftBarBtn.sizeToFit()
        leftBarBtn.tintColor = UIColor.white
        leftBarBtn.setTitle("        ", for: .normal)//扩大点击范围
        leftBarBtn.addTarget(self, action: #selector(dismissVC), for: .touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBarBtn)

    }

    func createNav() {
        self.navigationController?.navigationBar.setBackgroundImage( UIImage(named: "touming"), for: UIBarMetrics.default)
        
        self.navigationController?.navigationBar.shadowImage = UIImage()
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
        self.wl_navTitle("登录/注册", titleColor: ColorFromRGBHex_WL(0xffffff))

    }
    
    func createCV() {
        
        self.view.backgroundColor = UIColor.white
        //子壹财富main这没有背景图  后续如果有背景图直接打开替换图片即可
        self.view.addSubview(self.topBgImV)
        self.topBgImV.frame = CGRect(x: 0, y: 0, width: self.view.width_sd, height: self.view.width_sd*self.topBgImV.height_sd/self.topBgImV.width_sd)
        
        self.view.addSubview(self.phoneNumberView)
        self.phoneNumberView.center = self.view.center
        self.phoneNumberView.bounds = CGRect(x: 0, y: 0, width: self.view.width_sd-JCWLogRegTFMargin*2, height: JCWLogRegTFH)
        self.phoneNumberView.bottom_sd = self.topBgImV.bottom_sd - 86.aw_wl
        
        self.view.addSubview(self.logoImV)
        self.logoImV.centerX_sd = self.view.centerX_sd
        self.logoImV.bottom_sd = self.phoneNumberView.top_sd - 60
        
        self.view.addSubview(self.nextStepBtn)
        self.nextStepBtn.frame = CGRect(x: JCWLogRegTFMargin, y: self.topBgImV.bottom_sd+100.aw_wl, width: self.phoneNumberView.width_sd, height: 44)
        
        
        //监控按钮状态
        weak var weakSelf = self
        self.phoneNumberView.tF.reactive.continuousTextValues.observeValues { (text) in
            if text!.count > 11 {
                weakSelf?.phoneNumberView.tF.text = text!.substring(to: String.Index(encodedOffset: 11))
            }
            weakSelf?.nextStepBtn.isUserInteractionEnabled = text!.count >= 11
            weakSelf?.nextStepBtn.setBackgroundImage(UIImage(named: text!.count >= 11 ?"login_btn":"login_btn_gray"), for: .normal)
        }

    }
    //MARK:按钮点击事件
    @objc fileprivate func btnClick() {
        self.loadData()
    }
    
    //MARK:懒加载UI
    ///大背景
    fileprivate lazy var topBgImV: UIImageView = {
        let temp = UIImageView(image: UIImage(named: "login_bg"))
        temp.sizeToFit()
        return temp
    }()
    ///logo
    fileprivate lazy var logoImV: UIImageView = {
        let temp = UIImageView(image: UIImage(named: "login_logo"))
        temp.sizeToFit()
        temp.isHidden = true
        return temp
    }()
    ///手机号输入框
    fileprivate lazy var phoneNumberView: JCWAlertTextFieldView = {
        let temp = JCWAlertTextFieldView("请输入手机号码", "请输入您的手机号码", "login_phone")
        temp.tF.keyboardType = .numberPad
        return temp
    }()
    ///下一步按钮
    fileprivate lazy var nextStepBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setBackgroundImage(UIImage(named: "login_btn_gray"), for: .normal)
        temp.isUserInteractionEnabled = false
        temp.adjustsImageWhenHighlighted = false
        temp.setTitle("下一步", for: .normal)
        temp.titleLabel?.font = Font_WL(15)
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return temp
    }()
}


extension JCWLogInVC {
//    1：手机号不能为空
//    1：请输入正确的手机号
//    1：注册验证用户手机号是否可用失败
//    -1：未注册用户
//    0：已注册用户
    fileprivate func loadData() {
    
        self.view.jcw_showGifLoading()
        HYCNetService.checkMobile(withDict: ["mobile":self.phoneNumberView.tF.text ?? ""], onSuccess: { (resultDic) in
            self.view.hideHUDLoading(0)
            let vc = JCWLogInPswVC()
            vc.phoneNumber = self.phoneNumberView.tF.text
            self.navigationController?.pushViewController(vc, animated: true)
        }) { (code, errMsg) in
            if code == -1 {//手机号未注册
                self.view.hideHUDLoading(0)
                let vc = JCWRegVC()
                vc.phoneNumber = self.phoneNumberView.tF.text
                self.navigationController?.pushViewController(vc, animated:true)
            }else if code == 100 {//重新设置登录密码
                self.view.hideHUDLoading(0)
                let getPswVC = GetBackLoginPswVC()
                getPswVC.phoneNumber = self.phoneNumberView.tF.text ?? ""
                getPswVC.isShowGetBackPswAlertV = true
                self.pushVC(getPswVC)
            }else{
                self.view.showHUDFailTips(errMsg, hideDelay: kHUDHideDelay)
            }
        }
        
    }
}
